package v1

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"video-api/internal/model/entity"
	"video-api/internal/model/input"
	"video-api/internal/model/out"
	"video-api/utility/cdn_url"
)

type OrderInfoReq struct {
	g.Meta `path:"/" method:"post" summary:"获取订单详情" tags:"订单管理"`
	input.PageParam
}

type OrderInfoRes struct {
	Id          int64   `json:"orderId"`
	UserId      int64   `json:"userId" description:"用户ID"`                                                                       // 用户ID
	GoodsId     int64   `json:"goodsId" description:"商品ID"`                                                                      // 商品ID
	Status      string  `json:"status" description:"订单状态 pending-待支付,paying-支付中,completed-已完成,pay_fail-支付失败,system_cancel-系统取消"` // 订单状态 pending-待支付,paying-支付中,completed-已完成,pay_fail-支付失败,system_cancel-系统取消
	Platform    string  `json:"platform" description:"支付平台，必须在订单状态更新为支付中的同时设置值"`                                                 // 支付平台，必须在订单状态更新为支付中的同时设置值
	Amount      float64 `json:"amount"  description:"支付金额"`                                                                      // 支付金额
	PaymentTime string  `json:"paymentTime" description:"支付完成时间(成功或失败)"`                                                         // 支付完成时间(成功或失败)
	FailReason  string  `json:"failReason" description:"订单失败信息"`                                                                 // 订单失败信息 // 下单设备信息
	CreatedAt   string  `json:"createdAt" description:""`                                                                        //
	EpisodeId   int64   `json:"episodeId" description:"视频集id"`                                                                   // 视频集id
	// 商品信息
	Name      string `json:"name" description:"商品名称"`                    // 商品名称
	GoodsType string `json:"goodsType" description:"商品类型 VIP、COINS(积分)"` // 商品类型 VIP、COINS(积分)
	Coins     uint   `json:"coins" description:"积分价值"`                   // 积分价值
	Bonus     uint   `json:"bonus" description:"额外积分"`                   // 额外积分
}

func (r *OrderInfoRes) DTO(ctx context.Context, orders *entity.Orders) {
	if r == nil {
		g.Log().Warning(ctx, "OrderInfoRes-DTO is nil")
		return
	}
	r.Id = orders.Id
	r.UserId = orders.UserId
	r.GoodsId = orders.GoodsId
	r.Status = orders.Status
	r.Platform = orders.Platform
	r.Amount = orders.Amount
	// todo 需要改成适配海外时间
	r.PaymentTime = orders.PaymentTime.Format("Y-m-d H:i:s")
	r.FailReason = orders.FailReason
	// todo 需要改成适配海外时间
	r.CreatedAt = orders.CreatedAt.Format("Y-m-d H:i:s")
}

type UnlockDramaReq struct {
	g.Meta `path:"/unlock/shortDrama" method:"post" summary:"解锁剧集" tags:"订单管理"`
	// UserId       int64 `json:"userId" v:"required"`
	Coins        int   `json:"coins" description:"需要消耗的硬币（示例：10）"`
	EpisodeId    int64 `json:"episodeId" description:"短剧集数id（示例：1）"`
	ShortDramaId int64 `json:"shortDramaId" v:"required" description:"短剧id（示例：1）"`
}

type UnlockDramasReq struct {
	g.Meta `path:"/unlock/drama" method:"post" summary:"获取解锁剧集" tags:"订单管理"`
	input.PageParam
}

type UnlockDramaResp struct {
	EpisodeId       int64    `json:"episodeId"    description:"短剧集数id"`
	ShortDramaId    int64    `json:"shortDramaId" description:"短剧ID"`
	EpisodeTitle    string   `json:"episodeTitle" description:"章节标题"`
	EpisodeDes      string   `json:"episodeDes"   description:"短剧集的描述"`
	EpisodeOrder    int      `json:"order"        description:"短剧集的序列（第几集）"`
	DramaTitle      string   `json:"shortName"    description:"短剧标题"`
	DramaDes        string   `json:"description"  description:"短剧总简介"`
	DramaCoverImg   string   `json:"coverImg"     description:"短剧封面图片"`
	UnlockType      string   `json:"unlockType"        description:"single：单集解锁，all：全集解锁"`
	Coins           int      `json:"coins"             description:"消耗硬币"`
	EpisodeVideoUrl string   `json:"episodeVideoUrl"   description:"短剧集播放封面"`
	Tags            []string `json:"tags" description:"短剧标签"`
}

func (e *UnlockDramaResp) DTO(ctx context.Context, param *out.ShortDramaEpisodesDetail) {
	if e == nil {
		e = new(UnlockDramaResp)
	}
	if param != nil {
		e.EpisodeId = param.Id
		e.ShortDramaId = param.ShortDramaId
		e.EpisodeOrder = param.Order
		e.DramaDes = param.Description
		if param.ShortDrama != nil {
			e.EpisodeDes = param.ShortDrama.Description
			e.DramaTitle = param.ShortDrama.ShortName
			// 链接超时转换
			prefix := g.Cfg().MustGet(ctx, "cdnurl.url").String()
			dramaCoverImg, _ := cdn_url.CdnUrl(prefix, param.ShortDrama.CoverImg)
			e.DramaCoverImg = dramaCoverImg.Url
		}
	}
}

type OrderCreateReq struct {
	g.Meta `path:"/create" method:"post" summary:"创建订单" tags:"订单管理"`
	input.OrderCreate
}

type OrderCreateRes struct {
	*out.OrderEntity
}

type OrderPayReq struct {
	g.Meta `path:"/pay" method:"post" summary:"订单支付（用于回调）" tags:"订单管理"`
	input.OrderPay
}

type OrderPayRes struct {
	*out.OrderEntity
}
